<?php
namespace Home\Controller;
use Common\Controller\CommonController;
class LiuyanController extends HomeController {
 	//xuxiang
	public function _initialize(){
		
 		parent::_initialize();
	}
	/**
	 * 添加留言
	 */
	public function setLiuyan(){
		//是否登录
		//获取商品id
		//添加收藏
		$open_id = session('openid');
		$content = I('content');
		if($_FILES["pic1"]["name"]){
			$data['pic1']=$this->upload($_FILES["pic1"]);
		}
		$order_goods_id = I('order_goods_id');
		if(empty($order_goods_id)){
			$this->ajaxReturn(['status'=>-1,'msg'=>'系统繁忙，请稍后再试'],'JSONP');
		}
		$data['open_id'] = $open_id;
		$data['order_goods_id'] = $order_goods_id;
		$data['content'] = $content;
		$data['add_time'] = time();
		$data['status'] = 1;
		$re = M('Liuyan')->add($data);
		if($re){
			$this->ajaxReturn(['status'=>1,'msg'=>'请求成功'],'JSONP');
		}else{
			$this->ajaxReturn(['status'=>-2,'msg'=>'系统繁忙，请稍候再试'],'JSONP');
		}
	}
	/**
	 * 留言列表
	 */
	public function LiuyanList(){
		//是否登录
		//列出收藏列表
		$open_id = session('openid');
		$page = I('page')?I('page'):null;
		if(empty($page)){
			$page = 1;
		}
		$limit = 4;
		$pages = dealPage($page,$limit);
		$list = M('Liuyan')->where(array('open_id'=>$open_id))->limit($pages['begin'],$pages['over'])->select();
		foreach($list as $k=>$v){
			$list[$k]['goods'] = M('Order_goods')->where(array('id'=>$v['order_goods_id']))->find(); 
			$list[$k]['attr'] = M('Attr')->where(array('attr_id'=>$list[$k]['goods']['arrt_id']))->find();
		}
		$this->ajaxReturn($list,'JSONP');
	}
	
	/**
	 * 删除留言
	 */
	public function delLiuyan(){
		$open_id = session('openid');
		$liuyan_id = I('liuyan_id');
		if(empty($liuyan_id)){
			$this->ajaxReturn(['status'=>-1,'msg'=>'系统繁忙，请稍候再试'],'JSONP');
		}	
		$re = M('Liuyan')->where(array('liuyan_id'=>$liuyan_id))->delete();
		if($re){
			$this->ajaxReturn(['status'=>1,'msg'=>'请求成功'],'JSONP');
		}else{
			$this->ajaxReturn(['status'=>-2,'msg'=>'系统繁忙，请稍候再试'],'JSONP');
		}
	}
}